//
//  SPSelectCompanyView.m
//  panzer
//
//  Created by 还带大道 on 2018/10/23.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPSelectCompanyView.h"
#import "SPCommon.h"

@interface SPSelectCompanyView()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic,assign) NSInteger tempSelect_index;

@end

@implementation SPSelectCompanyView

+ (instancetype)shareHeaderView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = RZ_COLOR_ALPHA(0, 0, 0, 0.65);
}



- (void)setExpressList:(NSArray *)expressList
{
    _expressList = expressList;
    [self.pickerView reloadComponent:0];

}

- (void)setSelect_index:(NSInteger)select_index
{
    _select_index = select_index;
    _tempSelect_index = select_index;
    [self.pickerView selectRow: (select_index == -1)?0:select_index inComponent:0 animated:YES];
}


#pragma mark-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.expressList.count;
}


- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return self.width;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 38;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (!view){
        view = [[UIView alloc]init];
        UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, 38)];
        text.textAlignment = NSTextAlignmentCenter;
        text.text = self.expressList[row][@"text"];
        text.textColor = RZ_COLOR(51, 51, 51);
        text.font = [UIFont systemFontOfSize:21];
        [view addSubview:text];
        
        //隐藏上下直线
        [self.pickerView.subviews objectAtIndex:1].backgroundColor = RZ_COLOR(238, 238, 238);
        [self.pickerView.subviews objectAtIndex:2].backgroundColor = RZ_COLOR(238, 238, 238);
    }
    return view;
    
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _tempSelect_index = row;
}



- (IBAction)cancel {
    [self removeFromSuperview];
}


- (IBAction)ok {
    [self removeFromSuperview];
    if(_tempSelect_index == -1){
        _tempSelect_index = 0;
    }
    !self.selectBlock?:self.selectBlock(_tempSelect_index);
}

@end
